//
//  ESGTeamInquiryViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/8.
//  Copyright © 2020 QDSG. All rights reserved.
//

import HandyJSON

struct ESGTeamInquiryModel: HandyJSON {
    var id: String?
    var name: String?
    var leaderDoctorName: String?
    var leaderDoctorTitle: String?
    var hospitalName: String?
    var goodAtNames: String?
    var description: String?
    var score: String?
    var url: String?
    var thumbnailUrl: String?
    
    // MARK: - 详情页
    var detailDtoList: [DoctorReserveModel]?
}

struct ESGTeamInquiryViewModel {
    struct RowViewModel {
        let model: ESGTeamInquiryModel
        
        var teamId: String { return model.id ?? "" }
        var teamName: String { return model.name ?? "" }
        var teamLeaderName: String { return model.leaderDoctorName ?? "" }
        var teamLeaderOffice: String { return model.leaderDoctorTitle ?? "" }
        var hospitalName: String { return model.hospitalName ?? "" }
        /// 专长
        var specialty: String { return model.goodAtNames ?? "" }
        var teamDesc: String { return model.description ?? "" }
        var score: String { return model.score ?? "0" }
        /// 头像高清图
        var avatarHD: String { return model.url ?? "" }
        /// 头像标清图
        var avatarSD: String { return model.thumbnailUrl ?? "" }
        
        var teamMemberList: [DoctorReserveModel] { return model.detailDtoList ?? [] }
        
        func viewModel(for indexPath: IndexPath) -> DoctorReserveRowViewModel {
            return DoctorReserveRowViewModel(model: teamMemberList[indexPath.row])
        }
        
        func numberOfRows(in section: Int) -> Int {
            switch section {
            case 0: return 1
            case 1: return teamMemberList.count
            default: return 0
            }
        }
    }
    
    var models: [ESGTeamInquiryModel]
    
    var numberOfRows: Int { return models.count }
    
    func viewModel(for indexPath: IndexPath) -> RowViewModel {
        return RowViewModel(model: models[indexPath.row])
    }
    
    mutating func loadMore(with models: [ESGTeamInquiryModel]) {
        self.models += models
    }
    
    /// 获取团队问诊列表
    static func getTeamInquiryList(finished: @escaping (_ models: [ESGTeamInquiryModel], _ errorMsg: String?, _ code: HttpCode) -> Void) {
        HttpTool.shared.request(target: ApiManager.teamInquiryList, success: { (json, code) in
            if let models = JSONDeserializer<ESGTeamInquiryModel>.deserializeModelArrayFrom(json: json["data"].description) as? [ESGTeamInquiryModel] {
                finished(models, nil, .success)
            } else {
                finished([], json["message"].description, .failure)
            }
        }) { errorMsg in
            finished([], errorMsg, .failure)
        }
    }
    
    /// 获取团队问诊详情
    static func getTeamInquiryDetail(teamId: String,
                                     finished: @escaping (_ model: ESGTeamInquiryModel?, _ errorMsg: String?) -> Void)
    {
        let paras = ["id": teamId]
        HttpTool.shared.request(target: ApiManager.teamInquiryDetail(paras: paras), success: { (json, code) in
            let model = ESGTeamInquiryModel.deserialize(from: json["data"].description)
            finished(model, json["message"].description)
        }) { errorMsg in
            finished(nil, errorMsg)
        }
    }
}
